/* ========================================================================
   公共提交函数
  ========================================================================*/
(function($){

   'use strict';
    function ApplySubmit(element,api){
        this.element=element;
        this.options=this.element.data();
        this.form=$(this.options.form);
        this.postApi= api || 'apply_save.html';
        this.tokenApi='apply_token.html';
        this.mobileApi='apply_edit.html';
        this.locationApi='http://v.showji.com/Locating/showji.com2016234999234.aspx';
        this.ispass=true;

        if(!this.form.length){
          this.form =this.element.closest("form");
        }

    }

    ApplySubmit.prototype.handle=function(){//主方法
        this.data = this.form.serializeArray();

        /*提交前回调*/
        this.ispass=this.callBack(this.options.before,this); 
        if(this.ispass || typeof this.ispass =='undefined'){
          /*提交*/ 
          this.postData()
        }
    }
    ApplySubmit.prototype.callBack=function(type,parm,json){//前后回调

          if(typeof type !== 'undefined'){
          
            var callfun = type
            if (typeof callfun === 'string')
              callfun = callfun.toFunc();
            if (typeof callfun === 'function')
              callfun = callfun.apply(this,[parm,json])
          }  

          return callfun;
    }
    ApplySubmit.prototype.comSuccess=function(applyMobile){//公共回调
      var template= '<div class="apply_success"><h3>恭喜您抢到免费量尺设计服务！</h3><p>您用<em>'+applyMobile+'</em>的号码抢到免费量尺设计服务！</p>'+
                    '<p>您已获得<em>2000元</em>家具设计大礼包<em>（内含1800元设计套餐+200元代金券）</em>，我们的客服人员会在<em>24小时</em>内与您联系沟通，请您留意接听电话，祝您生活愉快!</p>'+
                    '</div>'
       this.form.addClass('comsuccess').append(template);
    }
    ApplySubmit.prototype.runScAfter=function(){//提交成功转化
        $("body").append('<iframe style="display:none" src="site_scafter.html?kid='+ACT_KID+'"><iframe/>');
    }

    ApplySubmit.prototype.postData=function(){//提交数据
         var _this=this;
         $.ajaxUrl('Post',_this.tokenApi,{},'json',false,function(res){
            if(res.code==200){
              _this.data.push(
                  {name:'token',value:res.result},
                  {name:'kid',value:ACT_KID}
                )
            }
        });
        $.ajaxUrl('Post',_this.postApi,this.data,'json',false,function(res){
          
          if(res.code==200){
            /*成功*/
             _this.runScAfter();
             if( typeof _this.options.comsuccess!=='undefined'){/*公用回调*/
              
                _this.comSuccess(res.result.mobile);
             }
             if(typeof _this.options.mobile!== 'undefined'){/*手机号码归属地*/
                    $.ajaxUrl('GET',_this.locationApi,{m:res.result.mobile,output:'json'},'jsonp',true,function(data){
                        var $locationData={
                              province:data.Province,
                              city:data.City,
                              id: res.result.id,
                        };
                        $.ajaxUrl('Post',_this.mobileApi,$locationData,'json',true);
                    })
             }
          }
          _this.callBack(_this.options.after,_this,res); 

        });
              
    }
    /*===插件定义===*/
    function Plugin() {
        return this.each(function () {
            var $this=$(this);
            var data=$this.data('apply');
            var api=$this.data('api');
            if(!data){
                $this.data('apply', (data=new ApplySubmit($this, api)))
            }
            data.handle();

        })
    }
  
    /*调用*/
    $.fn.ApplySubmit= Plugin;

})(window.jQuery || window.Zepto);

/* ========================================================================
   未提交报名函数
  ========================================================================*/

function uncommit(mobileinp){
    var uncommitApi= 'uncommit_save.html';
    var mobileApi='apply_edit.html';
    var locationApi='http://v.showji.com/Locating/showji.com2016234999234.aspx';
 
    mobileinp.blur(function(){
      var $form =$(this).closest("form");
      if($(this).val().isMobile()){//手机号码完整

            var $data = $form.serializeArray();
            $data.push({name:'kid',value:ACT_KID})

            $.ajaxUrl('Post',uncommitApi,$data,'json',true,function(res){
              if(res.code==200){
               /*手机号码归属地*/
                $.ajaxUrl('GET',locationApi,{m:res.result.mobile,output:'json'},'jsonp',true,function(data){
                    var $locationData={
                          province:data.Province,
                          city:data.City,
                          id: res.result.id,
                    };
                    $.ajaxUrl('Post',mobileApi,$locationData,'json',true);
                })
              }
            });

      }
    })
}

/*报名申请调用*/
(function(){

  var $applybtn=$(document).find("[data-toggle='applysubmit']");
  var $mobileinp=$(document).find("[data-toggle='uncommit']");

  /*报名提交*/
  $applybtn.Validate({
      success:function(obj){
        $(obj).ApplySubmit();
      }
  })
  /*未报名提交*/
  uncommit($mobileinp);


})(window.jQuery || window.Zepto)


